<template>
  <ul class="alpha">
    <li class="item" v-for="item of letters" :key="item" @click="handleClick" @touchstart="handleTouchstart" @touchmove="handleTouchmove" @touchend="handleTouchend" :ref="item">{{item}}</li>
  </ul>
</template>

<script>
export default{
  name: 'CityAlpha',
  props: ['cities'],
  data () {
    return {
      status: false,
      topA: 0,
      timer: null
    }
  },
  updated () {
    this.topA = this.$refs.A[0].offsetTop
  },
  methods: {
    handleClick (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchstart () {
      this.status = true
    },
    handleTouchmove (e) {
      if (this.status) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const y = e.touches[0].clientY - 79
          const index = Math.floor((y - this.topA) / 20)
          if (index >= 0 && index < this.letters.length) {
            this.$emit('change', this.letters[index])
          }
        }, 16)
      }
    },
    handleTouchend () {
      this.status = false
    }
  },
  computed: {
    letters () {
      const letters = []
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~style/varibles.styl'
  .alpha
    display:flex
    flex-direction:column
    justify-content:center
    position:absolute
    top:1.58rem
    right:0
    bottom:0
    width:.4rem
    .item
      color:$bgcolor
      text-align:center
      line-height:.4rem
</style>
